const config = require('_/config');
const mysql = require('mysql');

const pool = mysql.createPool({
  host: config.database.host,
  database: config.database.database,
  user: config.database.username,
  password: config.database.password,
  port: config.database.port,
  multipleStatements: true,
  connectionLimit: 999
});

let query = (sql, values) => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if(err) {
        reject(err);
      }else {
        connection.query(sql, values, (err, rows) => {
          if(err) {
            reject(err);
          }else {
            resolve(rows);
          }
          connection.release();
        })
      }
    })
  });
};

module.exports = {
  query
};

